package com.qianfeng.xinyihezi.xinyihezi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.model.LabelList;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Tcoy on 2016/11/14.
 */

public class TagRecyclerViewAdapter extends RecyclerView.Adapter<TagRecyclerViewAdapter.ViewHolder> {
    private IBackCallClick iBackCallClick;
    private Context context;
    private LayoutInflater inflater;
    private List<LabelList> mLabelLists;

    public TagRecyclerViewAdapter(Context context, List<LabelList> mLabelLists) {
        this.context = context;
        this.mLabelLists = mLabelLists;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public TagRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            viewHolder = new ViewHolder(inflater.inflate(R.layout.tag_item_grid_layout, null));
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvTagTitle.setText(mLabelLists.get(position).getName());
        Picasso.with(context).load(mLabelLists.get(position).getImage_url()).into(holder.imgIcon);

        holder.imgIcon.setTag(mLabelLists.get(position).getLabel_id());
        if (iBackCallClick != null) {
            holder.imgIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    iBackCallClick.onLabelId(v);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mLabelLists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgIcon;
        private TextView tvTagTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            imgIcon = (ImageView) itemView.findViewById(R.id.imageViewTagIcon);
            tvTagTitle = (TextView) itemView.findViewById(R.id.textViewTagTitle);
        }
    }

    public interface IBackCallClick {
        void onLabelId(View view);
    }

    public void setOnClick(IBackCallClick iBackCallClick) {
        this.iBackCallClick = iBackCallClick;
    }
}
